<?php

class ResetController extends ContentController 
{
	var $maxDepth = 10; //avoids loops possibly caused by symlinks
	var $sourceFileMustExists = false; 
	var $removeEmptyDirs = true;
	var $command = 'reset';
	
	function generateFile ()
	{
		$fileName = basename( $this->destinationFile );
		$basePath = $this->Controller->basePath;
		$this->log( 'attempting to kill '.$fileName.' in all subdirs of '.$basePath );
		$this->seekAndDestroySubdirs( $basePath, $fileName, $this->maxDepth );
		return true;
	}
	
	function seekAndDestroySubdirs ( $path, $fileName, $levels2go )
	{
		if ( $levels2go < 0 )
		{
			$this->log( 'MaxDepth ('.$this->maxDepth.') level excedeed in '.$path.'. Skipping.' );
			return false;
		}
		$CurrentDir = dir( $path );
		while (false !== ($entry = $CurrentDir->read())) 
		{
			$fullEntryName = $path.DIRECTORY_SEPARATOR.$entry;
			if ( $entry != '..' && $entry != '.' )
			{
//				$this->log( $fullEntryName.' [*]' );
				if ( is_dir( $fullEntryName ) )
				{
					$this->log( $fullEntryName.' [dir]' );
					$this->seekAndDestroy( $fullEntryName, $fileName, $levels2go - 1 );
					
				} 
			} else {
//				$this->log( $fullEntryName.' [r]' );
			}
		}
		$CurrentDir->close();
	}
	function seekAndDestroy ( $path, $fileName, $levels2go )
	{
		if ( $levels2go < 0 )
		{
			$this->log( 'MaxDepth ('.$this->maxDepth.') level excedeed in '.$path.'. Skipping.' );
			return false;
		}
		//$this->log ('---'.$path.' ??? '.$fileName);
		$CurrentDir = dir( $path );
		while (false !== ($entry = $CurrentDir->read())) 
		{
			$fullEntryName = $path.DIRECTORY_SEPARATOR.$entry;
			if ( $entry != '..' && $entry != '.' )
			{
//				$this->log( $fullEntryName.' [*]' );
				if ( is_dir( $fullEntryName ) )
				{
//					$this->log( $fullEntryName.' [dir]' );
					$this->seekAndDestroy( $fullEntryName, $fileName, $levels2go - 1 );
					
				} elseif ( is_file( $fullEntryName ))
				{
					// $this->log( $fullEntryName.' [file]' );
					if ( $entry == $fileName ) $this->unlink( $fullEntryName);		
				}
			} else {
//				$this->log( $fullEntryName.' [r]' );
			}
		}
		$CurrentDir->close();
		if ( $this->removeEmptyDirs && !$this->directoryCount( $path ) ) $this->rmdir( $path );
		
//		echo '<pre>'; var_dump( $fullEntryName ); echo '</pre>';
		//return false;
	}
	function directoryCount( $dirPath )
	{
		$count = 0;
		$CurrentDir = dir( $dirPath );
		while (false !== ($entry = $CurrentDir->read())) 
		{
			if ( $entry != '..' && $entry != '.' )
			{
				$count++;
			} 
		}
		$CurrentDir->close();
		return $count;
	}
	function unlink ( $filePath)
	{
		if ( $this->Controller->configuration['stopGeneration'])
		{
			$this->log ( '(simulated) deleting of: '.$filePath);
			return true;
		} 
		$unlinked =  @unlink( $filePath );
		$this->log ( 'deleting of: "'.$filePath.'" result:'.$unlinked );
		return $unlinked;
	}
	function rmdir ( $dirPath )
	{
		if ( $this->Controller->configuration['stopGeneration'])
		{
			$this->log ( '(simulated) deleting of directory: '.$dirPath);
			return true;
		}
		$removed = false;
		if ( is_writable( $dirPath ))
		{
			$removed =  @rmdir( $dirPath );	
		}
		$this->log ( 'deleting of directory: "'.$dirPath.'" result:'.$removed );
		return $removed;
	}
}
?>